/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "GameWorld.h"
#include "InputSystem.h"
#include "Application.h"
#include "CameraObject.h"
#include "WOWArchive.h"
#include "WorldMap.h"

namespace PQGame
{
	GameWorld::GameWorld(void)
	{
	}

	GameWorld::~GameWorld(void)
	{
	}

	GameWorld &GameWorld::getSingleton()
	{
		static GameWorld world;
		return world;
	}

	void GameWorld::init(PQEngine::SceneObject* sceneObject)
	{
		_sceneObject=sceneObject;

		initInputListener();
		initGameData();
		

		//choose a world space to load
		loadWorld(0);
		enterTile(33,29);
		initCamera();
	}

	void GameWorld::initCamera()
	{
		PQEngine::CameraObject* camera= _sceneObject->getCamera();
		float x=_map->getTileXPos();
		float y=_map->getTileMaxYPos();
		float z=_map->getTileZPos();
		camera->setPosition(50,50,50);
		camera->lookAt(0,0,0);
	}

	//set initial world tile
	void GameWorld::enterTile(int x,int y)
	{
		_map->enterTile(x,y);
	}

	void GameWorld::loadWorld(int index)
	{
		_map=WorldMap::getWorldMap(index);
	}

	void GameWorld::initGameData()
	{
		SWOWArchive.init();
	}

	void GameWorld::initInputListener()
	{
		InputSystem::getSingleton().addKeyListener(this,"MainWorldKeyListener");
		InputSystem::getSingleton().addMouseListener(this,"MainWorldMouseListener");
	}


	void GameWorld::update(float deltaT)
	{
		_map->update(deltaT);
	}

	bool GameWorld::keyPressed(const OIS::KeyEvent &e)
	{
		switch(e.key){
			case OIS::KC_ESCAPE:
				Application::getSingleton().shutdown();
				return false;
		}
		return true;
	}

	bool GameWorld::keyReleased( const OIS::KeyEvent &e)
	{
		return true;
	}

	bool GameWorld::mouseMoved( const OIS::MouseEvent &e)
	{
		return true;
	}

	bool GameWorld::mousePressed( const OIS::MouseEvent &e, OIS::MouseButtonID id)
	{
		return true;
	}
	
	bool GameWorld::mouseReleased( const OIS::MouseEvent &e, OIS::MouseButtonID id )
	{
		return true;
	}
}
